<?php
    namespace Home\Controller;
    use Think\Controller;
    class ChengbenluruController extends Controller {
    
    public function _initialize()
    {
        header("Access-Control-Allow-Origin: *");  

        header("Access-Control-Allow-Credentials: true"); 
      
        header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS"); //允许的请求类型
        
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept"); 

    }

    public function get_list()
    {

        $accounting_date = I('get.accounting_date');
        $cost_item_code = I('get.cost_item_code');
        if(!empty($accounting_date))
        {
            $accounting_date = strtotime($accounting_date);
        }
        $page = I("get.page");
        $limit = I("get.limit");
        

        // $data = $M->join("LEFT JOIN cbhs.cbhs_dept_cost_detail c,comm.sys_dept_attr_dict d")->where("c.dept_code = s.dept_code and d.id = s.dept_type")->field("s.dept_name,d.dept_type")->limit(($page-1)*$limit,$limit)->select();

        $sql = 'select s.dept_code,s.dept_name,c.uuid,c.costs,c.costs_armyfree,c.real_costs,c.remarks,c.flag,d.dept_type from comm.sys_dept_dict s left join comm.sys_dept_attr_dict d on   d.id = s.dept_type left join cbhs.cbhs_dept_cost_detail c on c.dept_code = s.dept_code';

        if(!empty($accounting_date))
        {
            $sql .= " and c.accounting_date = $accounting_date";
        }

        if(!empty($cost_item_code))
        {
            $sql .= " and c.cost_item_code = '$cost_item_code'";
        }
        $M = M("");
        $data = $M->query($sql);

        for ($i=0; $i < count($data); $i++) 
        { 
            if( empty($data[$i]['costs']))
            {
                $data[$i]['costs'] = 0;
            }
            if( empty($data[$i]['costs_armyfree']))
            {
                $data[$i]['costs_armyfree'] = 0;
            }
            if( empty($data[$i]['real_costs']))
            {
                $data[$i]['real_costs'] = 0;
            }
        }
        $json["data"] = $data;
        $json["code"] = 0;
        $json["count"] = 0;
        $json["msg"] = "";
        
        $this->ajaxReturn($json,"json");
    }

    public function get_one()
    {
        $uuid = I("post.uuid");

        $info = M("cbhs.cbhs_dept_cost_detail")->where("uuid = '$uuid'")->find();
        $info["dept_date"] = date("Y-m-d H:i:s",$info["dept_date"]);
        $json["data"]   = $info;
        $json["code"]   = 0;
        $json["msg"]    = "";

        $this->ajaxReturn($json,"json");
    }

    public function save_one()
    {
        $data = I("post.");
        $uuid = $data["uuid"];
        unset($data["uuid"]);

        $result = M("cbhs.cbhs_dept_cost_detail")->where("uuid = '$uuid'")->save($data);
        if($result !== false)
        {
            $json["code"] = 0;
            $json["msg"] = "修改成功";
        }
        else
        {
            $json["code"] = -1;
            $json["msg"] = "修改失败";
        }
 
        $this->ajaxReturn($json,"json");
    }

    public function add_one()
    {
        $data = I("post.");
        $uuid = uuid();
        
        $data["dept_date"] = time();
        $result = M("cbhs.cbhs_dept_cost_detail")->add($data);
        if($result !== false)
        {
            $json["code"] = 0;
            $json["msg"] = "添加成功";
        }
        else
        {
            $json["code"] = -1;
            $json["msg"] = "添加失败";
        }
 
        $this->ajaxReturn($json,"json");
    }

    public function set_field()
    {
        $data_all = I("post.");

        $accounting_date    = $data_all["accounting_date"];
        $item_code          = $data_all["item_code"];
        $uuid               = $data_all["uuid"];
        $dept_code          = $data_all["dept_code"];
        $cost_item_code     = $data_all["cost_item_code"];
        $field              = $data_all["field"];


        
        $where['accounting_date'] = strtotime($accounting_date);
        $where['dept_code'] = $dept_code;
        $where['cost_item_code'] = $cost_item_code;

        $info = M("cbhs.cbhs_dept_cost_detail")->where($where)->find();

        if(empty($info))
        {
            $where[$data_all['field']] = $data_all['value'];

            $result = M("cbhs.cbhs_dept_cost_detail")->add($where);
            
            $info = M("cbhs.cbhs_dept_cost_detail")->where($where)->find();

            $d['real_costs'] = $info['costs'] + $info['costs_armyfree'];

            M("cbhs.cbhs_dept_cost_detail")->where($where)->save($d);
        }
        else
        {
            $data[$data_all['field']] = $data_all['value'];

            $result = M("cbhs.cbhs_dept_cost_detail")->where($where)->save($data);
            $info = M("cbhs.cbhs_dept_cost_detail")->where($where)->find();

            $d['real_costs'] = $info['costs'] + $info['costs_armyfree'];
            M("cbhs.cbhs_dept_cost_detail")->where($where)->save($d);
        }
        
        if($result !== false)
        {
            $json["code"] = 0;
            $json["msg"] = "修改成功";
        }
        else
        {
            $json["code"] = -1;
            $json["msg"] = "修改失败";
        }
        $this->ajaxReturn($json,"json");
    }

    public function get_cbhs_cost_item_dict()
    {
        $list = M("cbhs.cbhs_cost_item_dict")->select();
        $json["data"]   = $list;
        $json["code"]   = 0;
        $json["msg"]    = "";
        $this->ajaxReturn($json,"json");
    }

}